<?php
require_once("../../../util/configuration/configuration_parametters.php");
require_once("../../../util/" . MESSAGES_PROPERTIES . "messages_model.php");
require_once("../../../util/exception/exception_handler.php");

require_once("../../autor_message/to/autor_message_to.php");
require_once("../../autor_message/dao/autor_message_dao_factory.php");

require_once("../../autor/to/autor_to.php");
require_once("../../autor/dao/autor_dao_factory.php");

class Autor_message_dao_test {

	private $dao;
	private $autor1;
	private $autor2;
	
	public function __construct() {}	
	
	public function before_test() {
		$this->dao = Autor_dao_factory::singleton()->get_dao();
		$this->autor1 = new Autor_to('xxxxxxxxxxxx','password','email');
		$this->autor2 = new Autor_to('xxxxxxxxxxxy','password','email');
		$this->dao->create($this->autor1);		
		$this->dao->create($this->autor2);
		$this->dao = Autor_message_dao_factory::singleton()->get_dao(); 
	}
	
	public function run_test() {		
		try {			
			$date = date("Y-m-d H:i:s");
			$autor_message_to = new Autor_message_to(0, $this->autor1->get_login(), $this->autor2->get_login(), 
														'title', 'content', $date, 'new');			
			echo 'autor_message: ';
			echo $autor_message_to->to_string();
			echo '<br/>';
			
			$autor_message_to = $this->dao->create($autor_message_to);
			echo 'create<br/>';
			
			if ($this->dao->exist($autor_message_to->get_number())) {
				
				$result = $this->dao->find_by_number($autor_message_to->get_number());
				echo 'find<br/>';
								
				$aux_autor_message_to = new Autor_message_to($result['number'], $result['from_autor_login'], $result['to_autor_login'],
																$result['title'], $result['content'], 
																$result['date'], $result['state']);
				
				if ($autor_message_to->equals($aux_autor_message_to)) {
					$autor_message_to->set_state('view');
					$this->dao->update($autor_message_to);
					echo 'update<br/>';
					
					$result = $this->dao->find_by_number($autor_message_to->get_number());
					$aux_autor_message_to = new Autor_message_to($result['number'], $result['from_autor_login'], $result['to_autor_login'],
																$result['title'], $result['content'], 
																$result['date'], $result['state']);
					echo 'autor_message: ';
					echo $aux_autor_message_to->to_string();
					echo '<br/>';		
				}
				
				$this->dao->remove($autor_message_to->get_number());
				echo 'remove<br/>';
				
			}		
		}
		catch (exception $e) {
			Exception_handler::singleton()->handler($e);
		}
	}
	
	public function after_test() {
		Autor_dao_factory::singleton()->get_dao()->remove($this->autor1->get_login());
		Autor_dao_factory::singleton()->get_dao()->remove($this->autor2->get_login());
		
	}
}

//test
$test = new Autor_message_dao_test();

$test->before_test();
$test->run_test();
$test->after_test();


?>